﻿<!doctype html>
<html>
<head>
    <title>SpreadJS - Async Function</title>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    
    <link href="../../../external/spreadjs/css/gcspread.sheets.excel2013white.9.40.20161.0.css" rel="stylesheet" type="text/css" />

    <script src="../../../external/external/jquery-1.8.2.min.js" type="text/javascript"></script>

    <script type="text/javascript" src="../../../external/spreadjs/gcspread.sheets.all.9.40.20161.0.min.js"></script>

    <script id="scriptInit" type="text/javascript">
    /*code_begin*/
    var spreadNS = GcSpread.Sheets;
    $(document).ready(function () {
        var spread = new GcSpread.Sheets.Spread(document.getElementById("ss"), { sheetCount: 1 });
        initSpread(spread);
    });

    function initSpread(spread) {
        var asyncSum = function () { };
        asyncSum.prototype = new spreadNS.Calc.Functions.AsyncFunction("ASUM", 1, 255);
        asyncSum.prototype.defaultValue = function () { return "Loading..."; };
        asyncSum.prototype.evaluateAsync = function (args, context) {
            // use setTimeout to simulate server side evaluation
            // in read world it maybe an ajax post to server for evaluation
            setTimeout(function () {
                var result = 0;
                for (var i = 0; i < args.length; i++) {
                    result += args[i];
                }
                result *= 2;
                context.SetAsyncResult(result);
            }, 2000);
        };

        var sheet = spread.sheets[0];
        sheet.allowCellOverflow(true);

        sheet.setArray(0, 0, [[5, 15]]);
        sheet.addCustomFunction(new asyncSum());

        sheet.setFormula(1, 1, "ASUM(A1,B1)");
        sheet.getCell(1, 1).foreColor("green");

        sheet.setValue(4, 0, "Edit the formula of B2 or referenced cell' value to see how async function works.");
    }
    /*code_end*/
    </script>
</head>
<body>
    <div class="sample-turtorial">
        <div id="ss" style="width:100%; height:410px;border: 1px solid gray;"></div>
    </div>
</body>
</html>